package com.dayaokeji.rhythmschoolstudent.client.home.join

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.rhythmschoolstudent.client.common.base.ActivityTemplateMethod
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils
import kotlinx.android.synthetic.main.activity_join_metting_and_course.*
import java.io.Serializable

class JoinMeetingAndCourseActivity : BaseActivity() , ActivityTemplateMethod{

    companion object {
        private const val SEARCH_TYPE = "search_type"

        fun navigation(cxt: Context,type: SearchType) {
            val intent = Intent(cxt, JoinMeetingAndCourseActivity::class.java)
            intent.putExtra(SEARCH_TYPE,type)
            cxt.startActivity(intent)
        }
    }

    private val joinCourseFragment: JoinCourseFragment by lazy { JoinCourseFragment.newInstance() }
    private val joinMeetingFragment: JoinMeetingFragment by lazy { JoinMeetingFragment.newInstance() }

    private var type: SearchType? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_join_metting_and_course)
        toolbar?.let {
            setSupportActionBar(it)
        }
        init()
    }

    override fun init() {
        type = intent.getSerializableExtra(SEARCH_TYPE) as SearchType
        super.init()
    }

    override fun setupView() {
        type?.let {
            when (it) {
                SearchType.MEETING -> {
                    supportActionBar?.title = "加入会议"
                    flAddCourseTips.visibility = View.GONE
                }
                SearchType.COURSE -> {
                    supportActionBar?.title = "加入课堂"
                }
            }
        }

    }

    override fun setupViewData() {
        type?.let {
            when (it) {
                SearchType.MEETING -> {
                    supportFragmentManager.beginTransaction().replace(R.id.fl_content,joinMeetingFragment).commit()
                }
                SearchType.COURSE -> {
                    supportFragmentManager.beginTransaction().replace(R.id.fl_content,joinCourseFragment).commit()
                }
            }
        }
    }

    /**
     * @click btnSearch
     */
    fun searchKey(view: View) {
        val key = etSearchKey.text.toString()
        if (TextUtils.isEmpty(key)) {
            ToastUtils.info("请输入查找的邀请码")
            return
        }
        flAddCourseTips.visibility = View.GONE
        type?.let {
            when (it) {
                SearchType.MEETING -> {
                    joinMeetingFragment.search(key)
                }
                SearchType.COURSE -> {
                    joinCourseFragment.search(key)
                }
            }
        }
    }

    enum class SearchType : Serializable{
        MEETING,
        COURSE
    }
}
